package com.elinshaw.pattern.event;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 支持并发
 */
public abstract class AsyncChannel<E extends Message> implements Channel<Event> {

    private final ExecutorService executorService;

    public AsyncChannel() {
        this(Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2));
    }


    public AsyncChannel(ExecutorService executorService) {
        this.executorService = executorService;
    }

    protected abstract void handle(Event message);

    @Override
    public final void dispatch(Event message) {
        executorService.submit(() -> this.handle(message));
    }
}
